#include "bbmemoapp.h"

#include "settingsadapter.h"

#include <bb/cascades/Container>
#include <bb/cascades/GroupDataModel>
#include <bb/cascades/ListView>
#include <bb/cascades/NavigationPane>
#include <bb/cascades/Page>
#include <bb/cascades/QmlDocument>
#include <bb/data/JsonDataAccess>

using namespace bb::data;

BBMemoApp::BBMemoApp()
{
    QmlDocument *qml = QmlDocument::create("asset:///main.qml");
    m_settingsAdapter = new SettingsAdapter(this);
    qml->setContextProperty("_settings", m_settingsAdapter);

    m_memoDb = new MemoDb();
    qml->setContextProperty("_db", m_memoDb);

    m_importer = new Importer(m_memoDb);
    qml->setContextProperty("_importer", m_importer);

    qml->setParent(this);
    NavigationPane *nav = qml->createRootObject<NavigationPane>();

    //ListView *stampList = nav->findChild<ListView*>("listView");
    //setUpCategoryListModelDataAccess(stampList);
    Application::instance()->setScene(nav);
}

void BBMemoApp::setUpCategoryListModelDataAccess(ListView *stampList)
{
    GroupDataModel *stampModel = new GroupDataModel(QStringList() << "category" << "categoryDesc");
    stampModel->setParent(this);
    stampModel->insertList(m_memoDb->getCategoriesModelItems());
    stampModel->setGrouping(ItemGrouping::ByFullValue);

    stampList->setDataModel(stampModel);

}


void BBMemoApp::setUpCategoryListModelJSon(ListView *stampList)
{
    JsonDataAccess jda;

    QVariantList mainList = jda.load("app/native/assets/items.json").value<QVariantList>();

    if (jda.hasError())
    {
        bb::data::DataAccessError error = jda.error();
        qDebug() << "JSON loading error: " << error.errorType() << ": " << error.errorMessage();
        return;
    }

    GroupDataModel *stampModel = new GroupDataModel(QStringList() << "category" << "categoryDesc");
    stampModel->setParent(this);
    stampModel->insertList(mainList);
    stampModel->setGrouping(ItemGrouping::ByFullValue);

    stampList->setDataModel(stampModel);
}
